require("../../usr/login/base64").Base64;

var t = getApp();

Page({
    data: {
        inited: !1,
        nameFocus: !0,
        mobileFocus: !1,
        addrFocus: !1,
        buyer: "",
        mobile: "",
        province: "",
        city: "",
        district: "",
        address: "",
        region: [],
        regionTxt: ""
    },
    changeVal: function(t) {
        var e = {}, a = t.currentTarget.dataset.name, i = t.detail.value;
        e[a] = i, this.setData(e);
    },
    bindRegionChange: function(t) {
        this.setData({
            region: t.detail.value,
            regionTxt: t.detail.value.join(" ")
        });
    },
    save: function() {
        if (!this.data.buyer) return this.alertAndFocus("请输入收货人", "nameFocus");
        if (!/^1[\d]{10}$/.test(this.data.mobile)) return this.alertAndFocus("联系方式不正确", "mobileFocus");
        if (!this.data.region || 0 === this.data.region.length) return this.alertAndFocus("请选择所在地区");
        if (!this.data.address) return this.alertAndFocus("请输入详细地址", "addrFocus");
        var e = {
            province: this.data.region[0],
            city: this.data.region[1],
            district: this.data.region[2],
            buyer: this.data.buyer,
            address: this.data.address,
            mobile: this.data.mobile
        };
        this.data.id && (e.address_id = this.data.id), e.userToken = this.data.usr.userToken, 
        t.request({
            url: t.server + "/userApi/modifyAddress",
            method: "POST",
            data: e,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                if (wx.showToast({
                    icon: "none",
                    title: t.data.msg.zh
                }), 0 === t.data.code) {
                    var e = getCurrentPages(), a = e[e.length - 2];
                    a && a.reload && a.reload(), wx.navigateBack();
                }
            }
        });
    },
    alertAndFocus: function(t, e) {
        var a = this;
        wx.showToast({
            icon: "none",
            title: t,
            complete: function() {
                if (e) {
                    var t = {};
                    t[e] = !0, a.setData(t);
                }
            }
        });
    },
    onLoad: function(t) {
        t.id && this.setData({
            id: t.id
        }), this.reload();
    },
    reload: function() {
        var e = this;
        this.setData({
            region: [],
            inited: !1
        });
        var a = wx.getStorageSync("usr");
        this.setData({
            usr: a
        }), t.request({
            url: t.server + "/userApi/getAddressList",
            method: "POST",
            data: {
                userToken: a.userToken
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var a = null;
                0 === t.data.code && (a = t.data.result.list[0], e.data.id && (a = t.data.result.list.filter(function(t) {
                    return t.id === e.data.id;
                })[0])), a && e.setData(a);
                var i = a ? [ a.province, a.city, a.district ] : [];
                e.setData({
                    region: a ? [ a.province, a.city, a.district ] : [],
                    regionTxt: i.length > 0 ? i.join(" ") : "",
                    inited: !0
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});